package com.qire.manhua.activity;

import android.os.Bundle;

import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.constant.CustomEvent;
import com.qire.common.constant.Navigation;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.utils.NoticeEventBus;
import com.qire.common.utils.NoticeEventBus.NoticeEvent;
import com.qire.common.utils.ThemeUtil;
import com.qire.manhua.R;
import com.qire.manhua.controller.TeensControlPanelController;
import com.qire.manhua.databinding.ActivityTeensControlPanelBinding;
import com.qire.manhua.model.viewModel.impl.TeensControlPanelViewModel;

import androidx.annotation.Nullable;

/**
 * 青少年模式控制面板
 */
@RouteMapping(path = Navigation.Page.TeensControlPanelPage)
public class TeensControlPanelActivity extends BaseActivity<ActivityTeensControlPanelBinding> {

    @Autowired(injector = ViewModelInjector.class)
    TeensControlPanelViewModel viewModel;

    @Autowired(injector = EventControllerInjector.class)
    TeensControlPanelController controller;

    @Override
    public int layoutRid() {
        return R.layout.activity_teens_control_panel;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeUtil.changeStatusBarLightOrDark(this, true, true);

        viewModel.refreshStatePanelUI();
        viewModel.queryTeensModeState();
    }

    @Override
    protected void registerObserver() {
        super.registerObserver();
        NoticeEventBus.NOTICE.subscribeEvent(CustomEvent.TEENS_MODE_STATE, this::onUpdateStateCallback);
    }

    @Override
    protected void unregisterObserver() {
        super.unregisterObserver();
        NoticeEventBus.NOTICE.unSubscribeEvent(CustomEvent.TEENS_MODE_STATE);
    }

    /**
     * 响应总线通知：来自{@link com.qire.manhua.dal.service.TeensModeService TeensModeService} 查询状态结果通知
     * @param event
     * @return boolean 事件是否需要继续传递给其他订阅者处理。true 不需要已处理了 false 需要
     */
    private boolean onUpdateStateCallback(NoticeEvent event) {
        viewModel.refreshStatePanelUI();
        return true;
    }

}
